package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IMemberCardService;
import com.younion.model.MyCardInfoEditResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.MemberCardVO;

public class MyCardInfoEditConsumer extends AbstractRequestConsumer {

	private IMemberCardService memberCardService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return editMyCardInfo(request, response);
	}

	public String editMyCardInfo(HttpServletRequest request,
			HttpServletResponse response) {
		MyCardInfoEditResult myCardInfoEditResult = new MyCardInfoEditResult();
		String card_id = request.getParameter("card_id");
		String memberName = request.getParameter("memberName");
		String memberSex = request.getParameter("memberSex");
		String memberBirth = request.getParameter("memberBirth");
		String memberPhone = request.getParameter("memberPhone");
		String memberEmail = request.getParameter("memberEmail");
		String memberAddress = request.getParameter("memberAddress");
		String memberQq = request.getParameter("memberQq");
		String membeIdCard = request.getParameter("membeIdCard");
		String isAllowPush = request.getParameter("isAllowPush");
		MemberCardVO memberCardVO = new MemberCardVO();
		ResultObject resultObject = new ResultObject();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			memberCardVO = memberCardService.getById(card_id);
			if(memberCardVO!=null){
				memberCardVO.setMemberName(memberName);
				if(StringUtils.isNotEmpty(memberSex)){
					memberCardVO.setMemberSex(new Integer(memberSex));
				}
				if(StringUtils.isNotEmpty(memberBirth)){
					memberCardVO.setMemberBirth(sdf.parse(memberBirth));
				}
				memberCardVO.setMemberPhone(memberPhone);
				memberCardVO.setMemberEmail(memberEmail);
				memberCardVO.setMemberAddress(memberAddress);
				memberCardVO.setMemberQq(memberQq);
				memberCardVO.setMembeIdCard(membeIdCard);
				if(StringUtils.isNotEmpty(isAllowPush))
					memberCardVO.setIsAllowPush(new Integer(isAllowPush));
				memberCardService.save(memberCardVO);
				myCardInfoEditResult.setSuccessful("1");
			}else{
				myCardInfoEditResult.setSuccessful("0");
				resultObject.setMsg("不存在此卡");
			}
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			myCardInfoEditResult.setSuccessful("0");
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		resultObject.setResult(myCardInfoEditResult);
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}
}
